package com.tju.resource.oauth.controller;

import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.Constants;
import com.tju.resource.model.SysPermission;
import com.tju.resource.server.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 权限CRUD
 *
 * @Author CLS
 * @Date 2021/12/31 14:15
 */
@Slf4j
@RestController()
public class SysPermissionController {

    private static final String prefix = "permission";

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private ChineseExceptionUtils zh_CH;


    /**
     * 查询菜单
     *
     * @param permission
     * @return
     */
    @PostMapping(prefix + "/list")
    @PreAuthorize("hasAuthority('sys:permission:list')")
    public Result<Object> list(@RequestBody SysPermission permission) {
        return sysPermissionService.selectSysPermissionList(permission);
    }

    /**
     * 删除菜单
     *
     * @param id 菜单id
     * @return
     */
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @PostMapping(prefix + "/delete/{id}")
    public Result<Object> list(@PathVariable("id") Long id) {
        if (sysPermissionService.selectCountPermissionByParentId(id) > 0) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.SubMenuExists);
        }
        if (sysPermissionService.selectCountRolePermissionByPermissionId(id) > 0) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MenuAssigned);
        }
        return sysPermissionService.deletePermissionByParentId(id);
    }

    /**
     * 新增
     */
    @PostMapping(prefix + "/add/{parentId}")
    public Result<Object> add(@PathVariable("parentId") Long parentId) {
        SysPermission permission = null;
        if (0L != parentId) {
            permission = sysPermissionService.SysPermissionByParentId(parentId);
        } else {
            permission = new SysPermission();
            permission.setId(0L);
            permission.setName("主目录");
        }
        return ResultUtil.success(ResultEnum.SUCCESS, permission);
    }

    /**
     * 新增保存菜单
     */
    @PreAuthorize("hasAuthority('sys:permission:add')")
    @PostMapping(prefix + "/add")
    public Result<Object> addSave(@RequestBody SysPermission permission) {
        if (Constants.MENU_NAME_NOT_UNIQUE.equals(sysPermissionService.checkPermissionNameUnique(permission))) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "新增菜单'" + permission.getName() + "'失败，菜单名称已存在");
        }else if(permission.getUserName()==null&&permission.getUserName()==""){
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "用户名为空");
        }
        return sysPermissionService.insertPermission(permission);
    }

    /**
     * 修改菜单
     */
    @PostMapping(prefix + "/edit/{id}")
    public Result<Object> edit(@PathVariable("id") Long id) {
        return ResultUtil.success(ResultEnum.SUCCESS, sysPermissionService.SysPermissionByParentId(id));
    }

    /**
     * 修改保存菜单
     */
    @PreAuthorize("hasAuthority('sys:permission:edit')")
    @PostMapping(prefix + "/edit")
    public Result<Object> editSave(@RequestBody SysPermission permission) {
        if (Constants.MENU_NAME_NOT_UNIQUE.equals(sysPermissionService.checkPermissionNameUnique(permission))) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "修改菜单'" + permission.getName() + "'失败，菜单名称已存在");
        }
        return sysPermissionService.updatePermission(permission);
    }

    /**
     * 选择菜单树
     */
    @PostMapping(prefix + "/selectPermissionTree/{id}")
    public Result<Object> selectMenuTree(@PathVariable("id") Long id) {
        return ResultUtil.success(ResultEnum.SUCCESS, sysPermissionService.SysPermissionByParentId(id));
    }


}
